package com.ckd.flyingtrip.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.NetworkError;
import com.android.volley.ParseError;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.App;

import java.util.ArrayList;

/**
 * Created by HITMAN on 2019/4/12.
 */
public class BaseActivity extends AppCompatActivity {


    ArrayList<Activity> activities = new ArrayList<Activity>();

    /**
     * 屏幕宽
     */
    public static int SCREEN_WIDTH;

    /**
     * 屏幕高
     */
    public static int SCREEN_HEIGHT;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager manager = getWindowManager();
        Display display = manager.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        addActivity();

    }

    protected void onDestroy() {
        super.onDestroy();
        removeActivity();
    }

    public void addActivity() {
        activities.add(this);
    }

    //
    public void removeActivity() {
        activities.remove(this);
    }

    ////
////    /**
////     * 结束所以活动
////     */
    public void finishAllAcitivity() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    /**
     * 跳转
     */
    public void startAction(@SuppressWarnings("rawtypes") Class cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /**
     * 跳转带数字
     */
    public void startActivityForResult_Value(@SuppressWarnings("rawtypes") Class cls, String data, int posin) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("data", data);
        startActivityForResult(intent, posin);
    }


    /**
     * 获取文字
     */
    public String getResString(int string) {
        return getResources().getString(string);
    }

    /**
     * 获取颜色
     */
    public int getResColor(int string) {
        return getResources().getColor(string);
    }

    /**
     * 获取Drawable
     */
    public Drawable getResDrawable(int string) {
        return getResources().getDrawable(string);
    }

    /**
     * 提示
     */
    public void toast(String str) {
        Toast.makeText(App.getContext(), str, Toast.LENGTH_LONG).show();
    }

    public void checkError(VolleyError error) {

        if (error != null) {
            if (error instanceof TimeoutError) {
                Toast.makeText(App.getContext(), "网络请求超时，请重试！", Toast.LENGTH_SHORT).show();
                return;
            }
            if (error instanceof ServerError) {
                Toast.makeText(App.getContext(), "服务器异常", Toast.LENGTH_SHORT).show();
                return;
            }
            if (error instanceof NetworkError) {
                Toast.makeText(App.getContext(), "请检查网络", Toast.LENGTH_SHORT).show();
                return;
            }
            if (error instanceof ParseError) {
                Toast.makeText(App.getContext(), "数据格式错误", Toast.LENGTH_SHORT).show();
                return;
            }

            Toast.makeText(App.getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();

        }

    }


    private Dialog loadingDialog;
    private long mPreClickTime;

    /**
     * @param @param  context activity
     * @param @param  msg 提示信息
     * @param @return
     * @return Dialog
     * @throws
     * @Title: createLoadingDialog
     * @Description: 等待提示框
     */
    @SuppressLint("InflateParams")
    @SuppressWarnings("deprecation")
    public Dialog createLoadingDialog(Context context, String msg,
                                      final boolean cancelable) {
        try {
            // 布局
            LayoutInflater inflater = LayoutInflater.from(context);
            // 得到加载view
            View v = inflater.inflate(R.layout.loading_dialog, null);
            // 加载布局
            LinearLayout layout = (LinearLayout) v
                    .findViewById(R.id.dialog_view);
            TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);
            tipTextView.setText(msg);
            // 创建自定义样式dialog
            loadingDialog = new Dialog(context, R.style.loading_dialog);
            // 是否可以用“返回键”取消
            loadingDialog.setCancelable(cancelable);
            // 设置布局
            loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            loadingDialog.show();
        } catch (Throwable e) {
            Log.e("dailog", "create dailog fail");
        }


        return loadingDialog;
    }

    /**
     * 隐藏等待提示框
     */
    public void dismisProgressDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (loadingDialog != null) {

                        loadingDialog.cancel();
                        loadingDialog.dismiss();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    Log.e("BaseActivity", "dismis progress dialog fail");
                }
            }
        });
    }
}
